package chessGame.utils;

import chessGame.Board;
import chessLib.Position;

import java.util.*;

/**
 * @BelongsProject: chessGame
 * @BelongsPackage: chessLib
 * @CreateTime: 2025-10-08  20:30
 * @Description: TODO
 * @Version: 1.0
 */
public class MoveUtils {
    //更具不同的moves 获取所有可移动的位置,这里的移动是只移动了一步，
    //需要根据规则进行修改。
    public static Collection<Position> pieceMoves(Position pos, int[][] moves) {
        ArrayList<Position> result = new ArrayList<Position>();
        for (int[] ms : moves) {
            int newX = pos.getRow() + ms[0];
            int newY = pos.getCol() + ms[1];

            if (newX > 8 || newX < 1 || newY > 8 || newY < 1)
                continue;

            result.add(new Position(newX, newY));
        }
        return result;
    }

    // 支持沿方向无限移动（直到边界），这里可以移动多步
    public static Collection<Position> multiStepMoves(Position pos, int[][] directions) {
        List<Position> result = new ArrayList<>();
        for (int[] dir : directions) {
            int step = 1;
            while (true) {
                int newRow = pos.getRow() + dir[0] * step;
                int newCol = pos.getCol() + dir[1] * step;
                // 检查是否超出棋盘边界
                if (newRow < 1 || newRow > 8 || newCol < 1 || newCol > 8) {
                    break;
                }
                result.add(new Position(newRow, newCol));
                step++;
            }
        }
        return result;
    }
    //去除掉已经占过的位置
    public static Set<Position> deduplication(Collection<Position> positions, Board board){
        Set<Position> moves = new HashSet<>();
        for (Position pos : positions) {
            if (!board.isOccupied(pos)) {
                moves.add(pos);
            }
        }
        return moves;
    }

}
